package com.iims.product.service;

import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.iims.common.constant.UserConstants;
import com.iims.product.domain.ProductType;
import com.iims.product.mapper.ProductTypeMapper;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 产品类型服务
 *
 * @author fanpei
 * @date 2021/6/11 14:52
 */
@Service
public class ProductTypeServiceImpl extends ServiceImpl<ProductTypeMapper, ProductType> implements IProductTypeService {

    @Override
    public List<ProductType> selectProductTypeList(ProductType protype) {
        return baseMapper.selectList(new LambdaQueryWrapper<ProductType>()
                .like(StrUtil.isNotBlank(protype.getProdTypeName()), ProductType::getProdTypeName, protype.getProdTypeName()));
    }

    @Override
    public ProductType selectOne(Long proTypeId) {
        return baseMapper.selectById(proTypeId);
    }

    @Override
    public String checkProductTypeNameUnique(ProductType productType) {
        Long ProductId = Validator.isNull(productType.getProdTypeId()) ? -1L : productType.getProdTypeId();
        ProductType info = getOne(new LambdaQueryWrapper<ProductType>()
                .eq(ProductType::getProdTypeName, productType.getProdTypeName()).last("limit 1"));
        if (Validator.isNotNull(info) && info.getProdTypeId().longValue() != ProductId.longValue()) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    @Override
    public int insertProductType(ProductType protype) {
        return baseMapper.insert(protype);
    }

    @Override
    public int updateProductType(ProductType protype) {
        return baseMapper.updateById(protype);
    }

    @Override
    public int deleteProductTypeById(Long proTypeId) {
        return baseMapper.deleteById(proTypeId);
    }

    @Override
    public boolean checkProductTypeUse(Long proTypeId) {
        return baseMapper.checkProductTypeUse(proTypeId) > 0;
    }

}
